package com.changgou.page.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.goods.feign.CategoryFeign;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.feign.SpuFeign;
import com.changgou.goods.pojo.Category;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.page.service.PageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PageServiceImpl implements PageService {

    @Autowired
    private TemplateEngine templateEngine;
    @Value("${pagepath}")
    String pagepath;

    //缺啥补啥
    @Override
    public void generateHtml(String spuId) {
        //2.补全数据
        Context context = new Context();
        Map<String, Object> itemData = this.getItemData(spuId);
        context.setVariables(itemData);

        //3.HTML放在那里
        //文件夹存不存在 不存在创建出来
        File file = new File(pagepath);
        if (!file.exists()) {
            file.mkdirs();
        }

        //文件名
        String htmlPath = pagepath + "/" + spuId + ".html";

        Writer writer = null;
        try {
            writer = new PrintWriter(new File(htmlPath));

            //1.数据+模板=HTML
            templateEngine.process("item", context, writer);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关流
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Autowired
    private SpuFeign spuFeign;
    @Autowired
    private CategoryFeign categoryFeign;
    @Autowired
    private SkuFeign skuFeign;

    //封装数据
    private Map<String, Object> getItemData(String spuId) {
        Map<String, Object> resultMap = new HashMap<>();

        //1.spu
        Spu spu = spuFeign.findSpuById(spuId).getData();
        resultMap.put("spu", spu);

        //图片处理
        if (spu != null && !StringUtils.isEmpty(spu.getImages())) {
            resultMap.put("imageList", spu.getImages().split(","));
        }

        //2.分类名称
        Category category1 = categoryFeign.findById(spu.getCategory1Id()).getData();
        resultMap.put("category1", category1);
        Category category2 = categoryFeign.findById(spu.getCategory2Id()).getData();
        resultMap.put("category2", category2);
        Category category3 = categoryFeign.findById(spu.getCategory3Id()).getData();
        resultMap.put("category3", category3);

        //3.sku
        List<Sku> skuList = skuFeign.findSkuListBySpuId(spuId);
        resultMap.put("skuList", skuList);

        //4.获取商品规格信息
        String specItems = spu.getSpecItems();
        Map map = JSON.parseObject(specItems, Map.class);
        resultMap.put("specificationList", map);

        return resultMap;
    }
}
